package com.dbumama.market.web.core.utils;

import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jianzhou.sdk.BusinessService;

/**
 * @author Javen
 *         2016年4月3日
 */
public class SMSUtils {
    /* 短信配置资源 */
    private static final Prop prop = PropKit.use("sms.properties");
    private final static String SMS_URL = "http://www.jianzhou.sh.cn/JianzhouSMSWSServer/services/BusinessService";
    /* 短信配置详情 */
    private static final String SMS_NAME = prop.get("name");
    private static final String SMS_PWD = prop.get("pwd");
    private static final String SMS_CONTENT_REGISTER_CODE = prop.get("content_register_code");
    private static final String SMS_CONTENT_FORGET_CODE = prop.get("content_forget_code");
    private static final String CONTENT_REGISTER_NOTIFY = prop.get("content_register_notify");
    private static final String CONTENT_ORDER_NOTIFY = prop.get("content_order_notify");
    private static final String SMS_CONTENT_VALIDATE_CODE = prop.get("content_validate_code");
    private static final String SMS_TYPE = prop.get("type");

    public enum SendSMSType {
        REGISTER, FORGET, ORDER_NOTIFY, REGISTER_NOTIFY,VALIDATE
    }

    /**
     * 发送短信验证码
     *
     * @param mobile
     * @param code
     * @param sendSMSType
     * @return
     */
    public static int SMSCode(String mobile, String code, String nickName, String courseName, String uesrMobile, String courseCount, SendSMSType sendSMSType) {
        String type = sendSMSType.name().toLowerCase();
        String content = "";
        int res_code = -9;
        try {
//			Map<String, String> queryParas=new HashMap<String, String>();
//			queryParas.put("name", SMS_NAME);
//			queryParas.put("pwd", SMS_PWD);
            if (type.equals("register")) {
                content = StringUtils.replace(SMS_CONTENT_REGISTER_CODE, "@", code);
            } else if (type.equals("forget")) {
                content = StringUtils.replace(SMS_CONTENT_FORGET_CODE, "@", code);
            } else if (type.equals("order_notify")) {
                content = StringUtils.replace(CONTENT_ORDER_NOTIFY, "@", nickName, courseName, courseCount);
            } else if (type.equals("register_notify")) {
                content = StringUtils.replace(CONTENT_REGISTER_NOTIFY, "@", nickName, uesrMobile);
            } else if(type.equals("validate")){
            	content = StringUtils.replace(SMS_CONTENT_VALIDATE_CODE, "@", code);
            }
            /*queryParas.put("content", );
            queryParas.put("mobile", mobile);
			queryParas.put("type", SMS_TYPE);*/

            BusinessService bs = new BusinessService();
            bs.setWebService(SMS_URL);
            int result = bs.sendBatchMessage(SMS_NAME, SMS_PWD, mobile, content + "【中和支付】");
            res_code = result;
        } catch (Exception e) {
        	e.printStackTrace();
            res_code = -10;
        }
        return res_code;
    }

    /**
     * 发送手机验证码并存入缓存
     *
     * @param mobile
     * @param smsType
     */
    public static String sendSMSCode(String mobile, int smsType) {
        String code;
        int res_code = -9;

        code = StringUtils.random(6, StringUtils.RandomType.INT);
        if (smsType == 0) {
            //注册
            res_code = SMSCode(mobile, code, null, null, null, null, SendSMSType.REGISTER);
        } else if (smsType == 1) {
            //找回密码
            res_code = SMSCode(mobile, code, null, null, null, null, SendSMSType.FORGET);
        } else if(smsType == 2){
            //验证异机登录
            res_code = SMSCode(mobile, code, null, null, null, null, SendSMSType.VALIDATE);
        }
        if (res_code < 0) {
            return null;
        }
        return code;
    }
}
